<?php
/**
 * Category
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/8
 * Time: 17:11
 */

namespace app\api\controller;

use app\admin\controller\SpecsValue;
use app\common\business\Category as CategoryBus;
use app\common\lib\Arr;
use app\common\lib\Show;

class Category extends ApiBase
{
    public function index(){
        //获取所有分类内容
        try{
            $categoryBus = new CategoryBus();
            $categorys = $categoryBus->getNormalAllCategorys();
        }catch (\Exception $e){
            //添加日志
            return Show::success([]);
        }

        $categorys = Arr::getTree($categorys);

        $sliceCategorys = Arr::sliceTreeArr($categorys);

        return show(config('status.success'),'OK',$sliceCategorys);
    }

    /**
     * 商品分类页——获取三级分类列表
     * @return \think\response\Json
     */
    public function search($cid){
        $categoryBusObj = new CategoryBus();
        //获取分类信息查看分类是否存在
        $cateinfo = $categoryBusObj->getById($cid);
        if(empty($cateinfo) || $cateinfo['status'] != config('status.success')){
            return  Show::error('分类不存在');
        }
        $result = [];
        //如果是一级分类
        if($cateinfo['pid'] == 0){
            $result['name'] = $cateinfo['name'];


            $result['focus_ids'] = [0,0];
            //获取二级分类
            $list2 = $categoryBusObj->getCateListByPid($cateinfo['id']);

            $result['list'] = [$list2['list'],[]];

        }else{  //如果不是一级分类
            //获取上级分类
            $pidCateinfo = $categoryBusObj->getById($cateinfo['pid']);
            if($pidCateinfo['pid'] == 0){ //上级分类pid=0是二级分类
                $result['name'] = $pidCateinfo['name'];

                $result['focus_ids'] = [$cateinfo['id'],0];

                //获取二级分类
                $list2 = $categoryBusObj->getCateListByPid($pidCateinfo['id']);
                //获取三级分类
                $list3 = $categoryBusObj->getCateListByPid($cateinfo['id'] );
                $result['list'] = [$list2['list'],$list3['list']];

            }else{ //三级分类
                //获取上级分类
                $pid2Cateinfo = $categoryBusObj->getById($cateinfo['pid']);
                //获取上上级分类
                $pid1Cateinfo = $categoryBusObj->getById($pidCateinfo);

                $result['name'] = $pid1Cateinfo['name'];

                $result['focus_ids'] = [$pid2Cateinfo['id'],$cateinfo['id']];

                $list2_id = $pid2Cateinfo['pid'];
                $list3_id = $pid1Cateinfo['pid'];
                $list2 = $categoryBusObj->getCateListByPid($list2_id);
                //获取三级分类
                $list3_id = empty($list3_id) ? $list2['id'] : $pid1Cateinfo['pid'];
                $list3 = $categoryBusObj->getCateListByPid($list3_id);

                $result['list'] = [$list2['list'],$list3['list']];

            }
        }

        return Show::success($result);
    }

    /**
     * 获取子分类
     * @param $cid
     * @return \think\response\Json
     */
    public function sub($cid){
        $categoryBusObj = new CategoryBus();

        $list = $categoryBusObj->getCateListByPid($cid);

        return Show::success($list['list']);
    }
}
